<?

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/

print '<ul id="top_nav"><li class="sel"><a href="admin.php">Панель администратора</a></li><li><a href="admin.php?name=articles">Управление статьями</a></li><li>Импорт статей</li></ul>';
simple_table ("[ <a href='admin.php?name=articles&act=add'>Добавить статью</a> ] [ <a href='admin.php?name=articles&ed=cat'>Управление разделами</a> ]");

include "engine/xml.class.php";
$xml = new XML ();

if ($_GET['import'] == "true") {

	if (isset($_FILES['xml_file']['name']) && $_FILES['xml_file']['name'] != "") {
		if (copy($_FILES['xml_file']['tmp_name'], "include/cache/".$_FILES['xml_file']['name'])) {
			$file = "include/cache/".$_FILES['xml_file']['name'];
			add_article ($file);
		}
		else site_error ("Не удалось скопировать файл на сервер");
	}
	if ($_POST['http_file'] != "") add_article ($_POST['http_file']);

}

function add_article ($xml_file) {
	global $xml, $GLOBAL_USER;
	
	$xml->xml_file = $xml_file;
	
	$options = $xml->select ("art_settings");
	$global_comments = $xml->select_from_text ("comments", $options);
	$global_public = $xml->select_from_text ("public", $options);
	$global_like_page = $xml->select_from_text ("like_page", $options);
	$global_visible = $xml->select_from_text ("visible", $options);
			
	if ($global_comments == "") $global_comments = "on";
	if ($global_public == "") $global_public = "yes";
	if ($global_like_page == "") $global_like_page = "no";
	if ($global_visible == "") $global_visible = "on";
			
	$i = 1;
	$errors = 0;
	$added = 0;
		
	foreach ($xml->select_array ("article") as $article) {
		$cat = trim($xml->select_from_text ("category", $article));
		$header = trim($xml->select_from_text ("header", $article));
		$desc = trim($xml->select_from_text ("description", $article));
		$body = trim($xml->select_from_text ("body", $article));
		$image = trim($xml->select_from_text ("categorys", $article));
		$url = trim($xml->select_from_text ("url", $article));
		$like_page = trim($xml->select_from_text ("like_page", $article));
		$comments = trim($xml->select_from_text ("comments", $article));
		$public = trim($xml->select_from_text ("bublic", $article));
		$visible = trim($xml->select_from_text ("visible", $article));
				
		$error = "";
		if ($cat == "") $error .= "<li>Не указана категория!";
		if ($header == "") $error .= "<li>Нет заголовка!";
		if ($body == "") $error .= "<li>Нет текста!";
		if ($error != "") { site_error ("При обработке $i статьи были обнаружены следующие ошибки:$error"); break; @unlink ($xml_file); }
				
		if ($public == "") $public = $global_public;
		if ($comments == "") $comments = $global_comments;
		if ($like_page == "") $like_page = $global_like_page;
		if ($visible == "") $visible = $global_visible;
		$author = $GLOBAL_USER['user_id'];
				
		$save = @mysql_query ("INSERT INTO ".DB_PEREFIX."_articles VALUES (0,
			'$cat',
			'$header',
			'$desc',
			'$body',
			'$author',
			'".time()."',
			0,
			'$comments',
			'$url',
			'$image',
			'$public',
			'$like_page',
			'$visible')");
		if ($save) $added++;
		else $errors++;
			
		$i++;				
	}
	@unlink ($xml_file);
			
	if ($errors != 0) site_error ("Визникла ошибка при добавлении $errors статьи(ей)".mysql_error());
	if ($added !=0) site_message ("Добавлена $added ститья(ей)");
}

past_table ("Импорт файла", '
<form method="post" action="admin.php?name=articles&ed=import&import=true" enctype="multipart/form-data">
Выберите файл на своем компьютере:<br />
<input type="file" name="xml_file" style="width: 300px;" /><p />
Или введите адрес в интерненте:<br />
<input type="text" name="http_file" style="width: 300px;" /><br />
<font color="silver">Например, http://site.com/articles.txt</font><p /><br />
<span class="important_button"><input type="submit" value="Импортировать статьи!" /></span>
</form>
');

come_back ("панели администратора", "admin.php");

?>
